﻿
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/JSAjaxFileUploader/JQuery.JSAjaxFileUploader.js"></script>
<form action="" id="imageForm" enctype="multipart/form-data" method="POST">
    <input type="file" name="userPhoto" id="userPhoto">
    <input type="button" value="上传" id="shangchuan">
</form>

<script>
    //上传头像
    $("#shangchuan").click(function () {
        var file = $("#userPhoto").val();
        if (file == "") {
            alert("请选择上传的头像");
            return;
        }
        else {
            //判断上传的文件的格式是否正确
            var fileType = file.substring(file.lastIndexOf(".") + 1);
            if (fileType != "png" && fileType != "jpg") {
                alert("上传文件格式错误");
                return;
            }
            else {
                var url = "http://localhost:35330/image/upload2";
                $.ajaxFileUpload({
                    url: url,
                    secureuri: false,
                    fileElementId: "userPhoto",    //file的id
                    dataType: "text",         //返回数据类型为文本
                    success: function (data, status) {
                        if (data == "1") {
                            alert("请上传宽度大于200像素和高度大于200像素的图片");
                        }
                        else if (data == "2") {
                            alert("请上传宽高比不超过2的图片");
                        }
                        else if (data == "3") {
                            alert("请上传文件大小不大于2M的图片");
                        }
                        else {
                            $("#uploadImage").hide();
                            $("#srcImg").attr("src", data);
                            $("#previewImg").attr("src", data);
                            $("#cutImage").show();
                            $("#bigImage").val(data);
                            cutImage();     //截取头像
                        }
                    }
                })
            }
        }
    })
</script>